﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK_HTTT_06.BUS;
using PTTK_HTTT_06.DAO;

namespace PTTK_HTTT_06.GUI.other
{
    public partial class MHThemNguyenLieu : UserControl
    {
        int i = 0;
        public MHThemNguyenLieu()
        {
            InitializeComponent();
        }

        private void MHThemNguyenLieu_Load(object sender, EventArgs e)
        {
            cmbDonViTinh.DisplayMember = "TenDonViTinh";
            cmbDonViTinh.ValueMember = "MaDonViTinh";
            cmbDonViTinh.DataSource = DON_VI_TINH.LoadDonViTinh();            
            DataTable dt = new DataTable();
            dt.Columns.Add("MaLoai",typeof(int));            
            dt.Columns.Add("TenLoai",typeof(string));
            dt.Rows.Add(0,"Dài hạn");
            dt.Rows.Add(1,"Tươi sống");
            cmbLoaiNL.DisplayMember = "TenLoai";
            cmbLoaiNL.ValueMember = "MaLoai";
            cmbLoaiNL.DataSource = dt;
            cmbLoaiNL.SelectedItem = cmbLoaiNL.Items.Count - 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = dgvNguyenLieu.RowCount;
            i--;
            if (txtTenNL.Text.ToString() != "")
            {
                if (HopLe(dgvNguyenLieu, txtTenNL.Text.ToString(), i) == 1)
                {
                    DataGridViewRow _row = new DataGridViewRow();
                    dgvNguyenLieu.Rows.Add(_row);
                    dgvNguyenLieu.Rows[i].Cells[0].Value = txtTenNL.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[1].Value = numericUpDown1.Value.ToString();
                    dgvNguyenLieu.Rows[i].Cells[2].Value = cmbDonViTinh.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[3].Value = cmbLoaiNL.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[4].Value = numericUpDown2.Value.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Nguyên liệu đã thêm bên dưới !");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin !");
                txtTenNL.Focus();
            }
        }

        private int HopLe(DataGridView data, string str, int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = DON_VI_TINH_DB.LoadDonViTinh();
            if (dgvNguyenLieu.Rows.Count > 1)
            {
                NGUYEN_LIEU _nguyenLieu = new NGUYEN_LIEU();
                int i = 0;
                while (dgvNguyenLieu.Rows.Count > 1)
                {
                    if (dgvNguyenLieu.Rows[i].Cells[0].Value == null)
                    {
                        dgvNguyenLieu.Rows.RemoveAt(i);
                    }
                    else
                    {
                        _nguyenLieu.TenNL = dgvNguyenLieu.Rows[i].Cells[0].Value.ToString();
                        _nguyenLieu.SoLuongTon = int.Parse(dgvNguyenLieu.Rows[i].Cells[1].Value.ToString());
                        string _temp = dgvNguyenLieu.Rows[i].Cells[2].Value.ToString();
                        foreach (DataRow item in dt.Rows)
                        {
                            if (_temp == item[1].ToString())
                            {
                                _nguyenLieu.MaDVT = int.Parse(item[0].ToString());
                            }
                        }
                        if (dgvNguyenLieu.Rows[i].Cells[3].Value.ToString() == "Dài hạn")
                        {
                            _nguyenLieu.LoaiNL= true;
                        }
                        else
                        {
                            _nguyenLieu.LoaiNL= false;
                        }
                        //_nguyenLieu.LoaiNguyenLieu = bool.Parse(dgvNguyenLieu.Rows[i].Cells[3].Value.ToString());
                        _nguyenLieu.HanSuDungToiDa = int.Parse(dgvNguyenLieu.Rows[i].Cells[4].Value.ToString());
                        NGUYEN_LIEU.ThemNguyenLieu(_nguyenLieu);
                        dgvNguyenLieu.Rows.RemoveAt(i);
                    }
                }
                if (dgvNguyenLieu.RowCount == 1)
                {
                    MessageBox.Show("Thêm thành công");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult _result = MessageBox.Show("Bạn có chắc muốn làm mới tất cả ?", "Xác nhận", MessageBoxButtons.OKCancel);
            if (_result == DialogResult.OK)
            {
                dgvNguyenLieu.Rows.Clear();
                txtTenNL.Text = "";
                txtTenNL.Focus();
            }
        }
    }
}
